package com.xiguaji.tiktok.v1.tiktok.ranking.repository;

import com.xiguaji.tiktok.v1.tiktok.ranking.model.Ranking;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
@Table
public interface RankingRepository extends CrudRepository<RankingRepository,Long> , RankingExtRepository {

    @Query("SELECT IFNULL(MAX(update_time),0) FROM ranking WHERE ranking_type = :rankingType ORDER BY update_time desc LIMIT 0,1")
    Long findUpdateTime(int rankingType);

    @Query("select DISTINCT(belong) from ranking where ranking_type = :rankingType and belong > :startDate ORDER BY belong  DESC")
    List<Integer> findBelongDate(int rankingType,int startDate);

    /**
     * 获取播主排名
     * @param uid
     * @param rankingType
     * @param bloggerType
     * @param date
     * @return
     */
    @Query("select * from ranking where uid = :uid and ranking_type = :rankingType and blogger_type = :bloggerType and belong = :date")
    Optional<Ranking> findBloggerRanking(long uid, int rankingType,int bloggerType, int date);
}
